package io.jboot.fly.directive;

import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
import io.jboot.fly.model.Post;
import io.jboot.fly.model.UserAction;
import io.jboot.fly.service.CategoryService;
import io.jboot.fly.service.PostService;
import io.jboot.fly.service.UserActionService;
import io.jboot.fly.service.UserService;
import io.jboot.utils.ArrayUtils;
import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;

import javax.inject.Inject;
import java.util.List;

@JFinalDirective("MyUploadCount")
public class MyUploadCount extends JbootDirectiveBase {

    @Inject
    UserActionService userActionService;

    @Override
    public void onRender(Env env, Scope scope, Writer writer) {
        String  postId = getParam("postId",scope);
        List<UserAction> ua =userActionService.findByPostAndAction(postId, UserAction.ACTION_UPLOAD);
        int uploadCount = ua!=null?ua.size():0;
        scope.setLocal("uploadCount", uploadCount);
        renderBody(env, scope, writer);
    }

    @Override
    public boolean hasEnd() {
        return true;
    }
}
